package com.eascs.admin.o2o.loan.controller;
/**
 * 贷款--Controller
 * @author ycz
 *
 */

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.badloan.entity.O2oCacleEntity;
import com.eascs.admin.o2o.badloan.service.O2oCancleServiceImpl;
import com.eascs.admin.o2o.internal.util.BaseUserUtil;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.LoanApplyFileForm;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.entity.OaLoanApplyEntity;
import com.eascs.admin.o2o.loan.form.AddLoanapplyForm;
import com.eascs.admin.o2o.loan.form.O2oLoanInfoForm;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loan.trans.O2oLoanUserTransFormart;
import com.eascs.admin.o2o.loanAccount.entity.O2oLoanapplyAccountEntity;
import com.eascs.admin.o2o.loanAccount.serviceimpl.O2oLoanapplyAccountImpl;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.common.custnew.entity.CustomerEntity;
import com.eascs.crm.common.custnew.service.CustomerService;
import com.eascs.crm.common.entity.Acq;
import com.eascs.crm.common.service.AcqService;
import com.eascs.crm.common.vo.ModelResult;
import com.eascs.crm.nsEditDetail.service.CustomerInfoService;
import com.eascs.gaczcontract.entity.CustomerBasicInfo;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.web.url.UrlHelper;
import com.eascs.workflow.common.entity.sequence.SequenceEntity;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sequence.SequenceService;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Controller
@RequestMapping("/o2oloanapply")
public class LoanapplyController extends BaseRestController {
	private static Logger log = LoggerFactory.getLogger(LoanapplyController.class);
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Reference
	CustomerService customerService;
	@Autowired
	O2oLoanUserService loanUserService;
	@Reference
	private SysOrgService sysOrgService;
	@Reference
	private SequenceService sequenceService;
	@Autowired
	private BaseUserUtil userUtils;
	@Autowired
	O2oCancleServiceImpl o2oCancleServiceImpl;
	@Autowired
	private CustomerInfoService customerInfoService;
	
	@Reference
	AcqService acqService;
	

	@Autowired
	O2oLoanapplyAccountImpl o2oLoanapplyAccountImpl;
	
	/**
	 * 查询客户信息--跳转到修改终端贷/品牌贷页面
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/find", method = RequestMethod.GET)
	public BaseRestResult<String> selectLoanApply(O2oLoanInfoForm form) {
		List<O2oLoanapplyEntity> list = o2oLoanapplyService.findByLoanId(form.getId());
		if (null != list && list.size() > 0) {
			for (O2oLoanapplyEntity o2oLoanapplyEntity : list) {
				O2oCacleEntity cacleEntity = o2oCancleServiceImpl.findByLoanapplyId(o2oLoanapplyEntity.getLoanapplyNo());
				if (null != cacleEntity && !StringUtil.isEmpty(cacleEntity.gettVetoTerm())) {
					Calendar cal = Calendar.getInstance();
					// 下面的就是把当前日期加一个月
					cal.setTime(cacleEntity.gettTime());
					cal.add(Calendar.MONTH, Integer.parseInt(cacleEntity.gettVetoTerm()));
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					String ttime = format.format(cal.getTime()); // 作废限制到期时间
					String nowtime = format.format(Calendar.getInstance().getTime());// 当前时间
					if (compare_date(ttime, nowtime) != -1) {
						// return BaseRestResult.warn("您好，您" +
						// format.format(o2oLoanapplyEntity.gettTime()) +
						// "申请的贷款单被否决，不能再限制贷款期限内贷款");
						return BaseRestResult.warn("该客户申请的贷款单（" + o2oLoanapplyEntity.getBillCode() + "）在" + format.format(cal.getTime()) + "被否决，限制期限是" + cacleEntity.gettVetoTerm() + "个月。");
					}
				}
			}
		}
		CustomerEntity basicEntity = customerService.queryCustomerEntityByCustomerId(form.getId());
		if (basicEntity == null) {
			return BaseRestResult.warn("客户不存在，请检查");
		}
		String checkLoanerResult = checkLoaner(basicEntity);
		if (!checkLoanerResult.equals(StatusContexts.SUCCESS)) {
			return BaseRestResult.warn(checkLoanerResult);
		}
		// 新系统借款人校验
		int o2oI = checkLoapply(form.getId());
		if (o2oI > 0) {
			return BaseRestResult.warn("该借款人在系统中已存在贷款单");
		}
		// 检查客户是否有担保的单为结清
		int count = loanUserService.getCheckCountD(basicEntity.getIdNumber());
		if (count > 0) {
			return BaseRestResult.warn("该客户有未结清的担保单，不能申请贷款");
		}

		log.info("进入check新系统中已是否可以申请end={}", o2oI);
		if ("2".equals(String.valueOf(basicEntity.getMaritalStatus()))) {
			CustomerEntity customer1 = customerService.queryCustomerEntityByCustomerId(basicEntity.getMateId());
			String checkMateResult = checkMate(customer1);
			if (!checkMateResult.equals(StatusContexts.SUCCESS)) {
				return BaseRestResult.warn(checkMateResult);
			}
			// 新系统配偶校验
			int o2oS = checkSpouse(basicEntity.getMateId());
			if (o2oS > 0) {
				return BaseRestResult.warn("该借款人的配偶在系统中已存在贷款单");
			}

			int countS = loanUserService.getCheckCountD(customer1.getIdNumber());
			if (countS > 0) {
				return BaseRestResult.warn("该客户配偶有未结清的担保单，不能申请贷款");
			}
		}

		return BaseRestResult.redirect(UrlHelper.build("/loanapply/addloan?customerId=" + form.getId() + "&loanType=" + form.getLoanType()));

	}

	/**
	 * 申请贷款-终端贷/品牌贷
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/saveloanapply", method = RequestMethod.POST)
	public BaseRestResult<String> addLoanApply(AddLoanapplyForm addloanapplyForm) {
		log.info("保存VO：AddLoanapplyForm=={}", addloanapplyForm.toString());
		if (StatusContexts.LOAN_TYPE_BP.equals(addloanapplyForm.getLoanType())||StatusContexts.LOAN_TYPE_BP_JX.equals(addloanapplyForm.getLoanType())||StatusContexts.LOAN_TYPE_SG.equals(addloanapplyForm.getLoanType())||StatusContexts.LOAN_TYPE_YC.equals(addloanapplyForm.getLoanType())||StatusContexts.LOAN_TYPE_SC.equals(addloanapplyForm.getLoanType())) {
			if (StringUtil.isEmpty(addloanapplyForm.getPartnerId())) {
				return BaseRestResult.warn("请选择渠道");
			}
		} else {
			if (StringUtil.isBlank(addloanapplyForm.getCustomerSdNo())) {
				return BaseRestResult.warn("请选客户名称");
			}
		}
		if (StringUtil.isEmpty(addloanapplyForm.getOperationModel())) {
			return BaseRestResult.warn("请选择经营模式");
		} else if ("ZY".equals(addloanapplyForm.getOperationModel())) {
			if (StringUtil.isEmpty(addloanapplyForm.getLtd1())) {
				return BaseRestResult.warn("请选中经营平台");
			}
		} else {
			if (StringUtil.isEmpty(addloanapplyForm.getLtd())) {
				return BaseRestResult.warn("请选中经营平台");
			}
		}

		CustomerEntity customer = customerService.queryCustomerEntityByCustomerId(addloanapplyForm.getCustomerId());
		if (customer == null) {
			return BaseRestResult.warn("crm中找不到该客户信息，请核实后在申请。");
		}
		log.info("在CRM中查询数据结果：用户信息{},配偶信息{}", customer.getCustomerName(), customer.getSpouse());
		// 新系统借款人校验
		int o2oI = checkLoapply(addloanapplyForm.getCustomerId());
		if (o2oI > 0) {
			return BaseRestResult.warn("该借款人在系统中已存在贷款单");
		}
		if (customer.getSpouse() != null) {
			CustomerEntity customerSpouse = customer.getSpouse();
			// 新系统配偶校验
			int o2oS = checkSpouse(customerSpouse.getCustomerId());
			if (o2oS > 0) {
				return BaseRestResult.warn("该借款人的配偶在系统中已存在贷款单");
			}
		}
		BigDecimal loanAmout = new BigDecimal(1000000.00);
		if (addloanapplyForm.getLoanaMount().compareTo(loanAmout) == 1) {
			return BaseRestResult.warn("申请金额不能超过100万");
		} else if (addloanapplyForm.getLoanaMount().compareTo(new BigDecimal(10000.00)) == -1) {
			return BaseRestResult.warn("申请金额不能小于1万");
		}
		/*
		 * if (basicEntity.getCustomerSd() == null) { return
		 * BaseRestResult.warn("请完善客户基本信息的门店名称字段,SD号不能为空！"); }
		 */
		ActOrgEntity actOrgEntity = null;
		if (!StringUtil.isEmpty(addloanapplyForm.getLtd1())) {
			addloanapplyForm.setLtd(addloanapplyForm.getLtd1());
			actOrgEntity = sysOrgService.getOrgByUnitNo(addloanapplyForm.getLtd());
		} else {
			actOrgEntity = sysOrgService.getOrgByUnitNo(addloanapplyForm.getLtd());
		}
		O2oLoanapplyEntity o2oLoanapplyEntity = new O2oLoanapplyEntity();
		o2oLoanapplyEntity.setCreatRo(userUtils.getCreateOr());
		o2oLoanapplyEntity.setCreateDate(new Date());
		o2oLoanapplyEntity.setEndRo(userUtils.getCreateOr());
		o2oLoanapplyEntity.setEndDate(new Date());
		o2oLoanapplyEntity.setCreditstatus(1);
		o2oLoanapplyEntity.setIslock(StatusContexts.O2O_NO);
		o2oLoanapplyEntity.setLoanType(addloanapplyForm.getLoanType()); // 插入贷款类型
		// 页面上加一个客户门店屌用sd的接口

		// 3240245,凤凰山粮油超市,SD.3240245,XJ凤凰山粮油超市{
		if (StatusContexts.LOAN_TYPE_BK.equals(addloanapplyForm.getLoanType())) {
			o2oLoanapplyEntity.setCustomerCode(addloanapplyForm.getCustomerCode());
			o2oLoanapplyEntity.setCustomeRname(addloanapplyForm.getCustomerName());
			o2oLoanapplyEntity.setCustomerNo(addloanapplyForm.getCustomerSdNo());
		}

		o2oLoanapplyEntity.setLoanId(customer.getCustomerId());
		o2oLoanapplyEntity.setLoanFlg(StatusContexts.O2O_NO);
		o2oLoanapplyEntity.setWfType(StatusContexts.O2O_NEW);
		o2oLoanapplyEntity.setBankFlg(StatusContexts.O2O_NO);
		o2oLoanapplyEntity.setEndDate(new Date());
		o2oLoanapplyEntity.setContractFlag(StatusContexts.O2O_NO);
		o2oLoanapplyEntity.setDcFlag(StatusContexts.O2O_NO);// 标记是否代偿
		o2oLoanapplyEntity.setLoanrate(new BigDecimal(0.0800));
		o2oLoanapplyEntity.setManagefeerate(new BigDecimal(0.0600));
		o2oLoanapplyEntity.setDepositrate(new BigDecimal(0.0500));
		o2oLoanapplyEntity.setContractFlag(StatusContexts.O2O_NO);
		o2oLoanapplyEntity.setBcFlag(StatusContexts.O2O_NO);
		o2oLoanapplyEntity.setIsPlaceOnFile(StatusContexts.O2O_NEW);
		o2oLoanapplyEntity.setLoanaMount(addloanapplyForm.getLoanaMount());
		o2oLoanapplyEntity.setLtd(addloanapplyForm.getLtd());
		o2oLoanapplyEntity.setLtdName(actOrgEntity.getOrgName());
		o2oLoanapplyEntity.setBuybackId(addloanapplyForm.getBuybackId());
		o2oLoanapplyEntity.setOperationModel(addloanapplyForm.getOperationModel());
		o2oLoanapplyEntity.setBuybackName(addloanapplyForm.getBuybackName());
		if (addloanapplyForm.getLoanTerm() != null) {
			o2oLoanapplyEntity.setLoanTerm(addloanapplyForm.getLoanTerm());
		}
		if (StatusContexts.LOAN_TYPE_BP.equals(addloanapplyForm.getLoanType())||StatusContexts.LOAN_TYPE_BP_JX.equals(addloanapplyForm.getLoanType())||StatusContexts.LOAN_TYPE_SG.equals(addloanapplyForm.getLoanType())||StatusContexts.LOAN_TYPE_YC.equals(addloanapplyForm.getLoanType())||StatusContexts.LOAN_TYPE_SC.equals(addloanapplyForm.getLoanType())) {
			o2oLoanapplyEntity.setPartnerId(addloanapplyForm.getPartnerId());
			ModelResult<Acq> model = acqService.selectAcq(addloanapplyForm.getPartnerId());
			if (model.isSuccess()) {
				Acq acq = model.getModel();
				o2oLoanapplyEntity.setPartnerCode(acq.getAcqCode());
				o2oLoanapplyEntity.setPartnerName(acq.getAcqName());
			} else {
				o2oLoanapplyEntity.setPartnerName(addloanapplyForm.getPartnerName());
			}
		}
		o2oLoanapplyEntity.setOperatorNo(addloanapplyForm.getOperatorNo());
		o2oLoanapplyEntity.setOperatorName(addloanapplyForm.getOperatorName());
		o2oLoanapplyEntity.setRecommendType(addloanapplyForm.getRecommendType());
		o2oLoanapplyEntity.setReferrerNo(addloanapplyForm.getReferrerNo());
		o2oLoanapplyEntity.setReferrerName(addloanapplyForm.getReferrerName());
		o2oLoanapplyEntity.setRepayMentway(addloanapplyForm.getRepayMentway());
		o2oLoanapplyEntity.setGuarAnteeWay(addloanapplyForm.getGuarAnteeWay());
		o2oLoanapplyEntity.setBillCode(buildCode(actOrgEntity.getUnitCode(), addloanapplyForm.getLoanType()));
		o2oLoanapplyEntity.setLtdCode(actOrgEntity.getUnitCode());
		o2oLoanapplyEntity.setCommiceRecommend(addloanapplyForm.getCommiceRecommend());
		o2oLoanapplyEntity.setIsNewNs("Y");
		if (!StringUtil.isEmpty(addloanapplyForm.getRemark()))
			o2oLoanapplyEntity.setRemark(addloanapplyForm.getRemark().trim());
		O2oLoanUserEntity o2oLoanUserEntity = O2oLoanUserTransFormart.customerNewToloanUser(new O2oLoanUserEntity(), customer, o2oLoanapplyEntity.getLoanapplyNo());
		O2oLoanapplyAccountEntity o2oLoanapplyAccountEntity = new O2oLoanapplyAccountEntity();
		if (customer != null && customer.getSpouse() != null) { // 判断是否有配偶
			CustomerEntity customerSpouse = customer.getSpouse();
			O2oLoanUserEntity o2oLoanUserSpouseEntity = O2oLoanUserTransFormart.customerNewToloanUser(new O2oLoanUserEntity(), customerSpouse, o2oLoanapplyEntity.getLoanapplyNo());
			o2oLoanUserSpouseEntity.setSpouseId(o2oLoanUserEntity.getLoanId());
			o2oLoanapplyService.saveEntityById(o2oLoanapplyEntity, o2oLoanUserEntity, o2oLoanUserSpouseEntity,o2oLoanapplyAccountEntity);
		} else {
			o2oLoanapplyService.saveEntityById(o2oLoanapplyEntity, o2oLoanUserEntity,o2oLoanapplyAccountEntity);
		}
		//更新customer_basic_info剩余的信息，根据身份证同步最新贷款人的门店信息、资产信息
		customerInfoService.sychCustomerInfo(o2oLoanUserEntity.getId(),  o2oLoanapplyEntity.getLoanapplyNo(), o2oLoanUserEntity.getDocumentNo());
		return BaseRestResult.redirect(UrlHelper.build("/loanapply/findloan?loanapplyNo=" + o2oLoanapplyEntity.getLoanapplyNo()));
	}

	/**
	 * 修改申请贷款
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/updateloan", method = RequestMethod.POST)
	public BaseRestResult<String> updateLoanApply(LoanApplyFileForm loanApplyFileEntity) {
     
      
		O2oLoanapplyEntity entity = o2oLoanapplyService.findById(loanApplyFileEntity.getLoanapplyNo());
		
		if (StatusContexts.LOAN_TYPE_BP.equals(loanApplyFileEntity.getLoanType())||StatusContexts.LOAN_TYPE_BP_JX.equals(loanApplyFileEntity.getLoanType())||StatusContexts.LOAN_TYPE_SG.equals(loanApplyFileEntity.getLoanType())||StatusContexts.LOAN_TYPE_YC.equals(loanApplyFileEntity.getLoanType())||StatusContexts.LOAN_TYPE_SC.equals(loanApplyFileEntity.getLoanType())) {
			if (StringUtil.isEmpty(loanApplyFileEntity.getPartnerId())) {
				return BaseRestResult.warn("请选择渠道");
			} else {
				entity.setPartnerId(loanApplyFileEntity.getPartnerId());
				ModelResult<Acq> model = acqService.selectAcq(loanApplyFileEntity.getPartnerId());
				if (model.isSuccess()) {
					Acq acq = model.getModel();
					entity.setPartnerCode(acq.getAcqCode());
					entity.setPartnerName(acq.getAcqName());
				} else {
//					o2oLoanapplyEntity.setPartnerName(addloanapplyForm.getPartnerName());
					return BaseRestResult.warn("渠道保存异常，没有找到此渠道！");
				}
			}
		}
		if (StatusContexts.LOAN_TYPE_BK.equals(loanApplyFileEntity.getLoanType())) {
			if (StringUtil.isBlank(loanApplyFileEntity.getCustomerSdNo())) {
				return BaseRestResult.warn("请选客户名称：");
			} else {
				entity.setCustomerCode(loanApplyFileEntity.getCustomerCode());
				entity.setCustomeRname(loanApplyFileEntity.getCustomerName());
				entity.setCustomerNo(loanApplyFileEntity.getCustomerSdNo());
			}
		}
		if ("ZY".equals(loanApplyFileEntity.getOperationModel())) {
			if (StringUtil.isEmpty(loanApplyFileEntity.getLtd1())) {
				return BaseRestResult.warn("请选中经营平台");
			}
		} else {
			if (StringUtil.isEmpty(loanApplyFileEntity.getLtd())) {
				return BaseRestResult.warn("请选中经营平台");
			}
		}
		
		if (loanApplyFileEntity.getLoanaMount()==null) {
			return BaseRestResult.warn("申请金额不能为空");
		}


		BigDecimal loanAmout = new BigDecimal(1000000.00);
		if (loanApplyFileEntity.getLoanaMount().compareTo(loanAmout) == 1) {
			return BaseRestResult.warn("申请金额不能超过100万");
		} else if (loanApplyFileEntity.getLoanaMount().compareTo(new BigDecimal(10000.00)) == -1) {
			return BaseRestResult.warn("申请金额不能小于1万");
		}
		User user = UserHolder.getUser();
		entity.setEndRo(user.getNo());
		ActOrgEntity actOrgEntity = null;
		if ("ZY".equals(loanApplyFileEntity.getOperationModel())) {
			loanApplyFileEntity.setLtd(loanApplyFileEntity.getLtd1());
			actOrgEntity = sysOrgService.getOrgByUnitNo(loanApplyFileEntity.getLtd());
		} else {
			actOrgEntity = sysOrgService.getOrgByUnitNo(loanApplyFileEntity.getLtd());
		}
		if (entity != null && entity.getBillCode() == null) {
			entity.setBillCode(buildCode(actOrgEntity.getUnitCode(), loanApplyFileEntity.getLoanType()));
		}
		Date date = new Date();
		entity.setLoanaMount(loanApplyFileEntity.getLoanaMount());
		entity.setLoanapplyNo(loanApplyFileEntity.getLoanapplyNo());
		entity.setEndDate(date);
		entity.setLoanType(loanApplyFileEntity.getLoanType());
		entity.setLtd(loanApplyFileEntity.getLtd());
		entity.setLtdCode(actOrgEntity.getUnitCode());
		entity.setLtdName(actOrgEntity.getOrgName());
		if (loanApplyFileEntity.getLoanTerm() != null) {
			entity.setLoanTerm(Integer.parseInt(loanApplyFileEntity.getLoanTerm()));
		} else {
			entity.setLoanTerm(0);
		}
		entity.setOperatorNo(loanApplyFileEntity.getOperatorNo());
		entity.setOperatorName(loanApplyFileEntity.getOperatorName());
		entity.setRepayMentway(loanApplyFileEntity.getRepayMentway());
		entity.setGuarAnteeWay(loanApplyFileEntity.getGuarAnteeWay());
//		entity.setReferrerNo(loanApplyFileEntity.getReferrerNo());
//		entity.setReferrerName(loanApplyFileEntity.getReferrerName());
		entity.setCommiceRecommend(loanApplyFileEntity.getCommiceRecommend());
		entity.setOperationModel(loanApplyFileEntity.getOperationModel());
		if (!StringUtil.isEmpty(loanApplyFileEntity.getRemark()))
			entity.setRemark(loanApplyFileEntity.getRemark().trim());
		o2oLoanapplyService.updateFormApplyLoan(entity);
		return BaseRestResult.info("保存成功").andRedirect(UrlHelper.build("/oldo2olike/detail_oldlike/zx?loanapplyNo=" + entity.getLoanapplyNo()));
	}

	/**
	 * 校验是否可以申请贷款
	 * 
	 * @return
	 */
	public int checkLoapply(String id) {
		return o2oLoanapplyService.checkLoapply(id);
	}

	public int checkSpouse(String id) {
		return o2oLoanapplyService.checkSpouse(id);
	}

	/**
	 * 校验老系统是否可以申请贷款
	 * 
	 * @return
	 */
	public int checkOALoapply(OaLoanApplyEntity applyEntity) {
		return o2oLoanapplyService.checkOALoapply(applyEntity);
	}

	public int checkOaSpouse(OaLoanApplyEntity applyEntity) {
		return o2oLoanapplyService.checkOaSpouse(applyEntity);
	}

	/**
	 * 生成贷款编号
	 * 
	 * @param code
	 * @param type
	 * @return
	 */
	public String buildCode(String code, String type) {
		String bill = StringUtils.getFourBitYearMonth(new Date());
		String prefix = code + "-" + type + "-" + bill;
		String flag = sequenceService.getSeq(SequenceEntity.O2O_LOAN_NO, StatusContexts.O2O_SEQUENCE_LENGTH, prefix);
		if (flag == null) {
			throw new RuntimeException();
		}
		return flag;
	}

	@RequestMapping(value = "/datalist")
	public PaginationRestResult getOaBasicData(String loanApplyNo,String readonly) { //readonly 是否只读
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		PageResult<Object[]> pageResult = o2oLoanapplyService.findO2oDataListEntityById(loanApplyNo);
		Button[] titleButtons = new Button[0];
		if(StringUtil.isBlank(readonly)){//为空的默认给显示
			readonly ="false";
		}
		if("false".equals(readonly)){
			titleButtons = new Button[] { null, ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		}
		PaginationInfo datalistInfo = new PaginationInfo(pageResult, titles, "");
		datalistInfo.setTitleButtons(titleButtons);
		datalistInfo.setBuildPagingNavigation(false);
		datalistInfo.setFirstRenderColumnIndex(1);
		datalistInfo.setBuildRowNumber(true);
		datalistInfo.setRendTo("#o2o_file");
		return PaginationRestResult.buildPaginationRestResult(datalistInfo);
	}
	
	
	@RequestMapping(value = "/getFqzxbg")
	public PaginationRestResult getFqzxbg(String userZxId,String mateZxId) {
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		PageResult<Object[]> pageResult = o2oLoanapplyService.findFqZxbgfj(userZxId, mateZxId);
		Button[] titleButtons = new Button[] { null, ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo datalistInfo = new PaginationInfo(pageResult, titles, "");
		datalistInfo.setTitleButtons(titleButtons);
		datalistInfo.setBuildPagingNavigation(false);
		datalistInfo.setFirstRenderColumnIndex(1);
		datalistInfo.setBuildRowNumber(true);
		datalistInfo.setRendTo("#o2o_file");
		return PaginationRestResult.buildPaginationRestResult(datalistInfo);
	}

	@RequestMapping(value = "/byCodeDatalist")
	public PaginationRestResult getByCodeData(String billCode, String code) {
		log.info("查询回款凭证开始，参数：billCode:【{}】，code:【{}】", billCode, code);
		O2oLoanapplyEntity loanEntity = new O2oLoanapplyEntity();
		if (billCode.contains("O2O")) {
			OaLoanApplyEntity oaLoanApplyEntity = o2oLoanapplyService.findOaLoanByCode(billCode);
			loanEntity.setLoanapplyNo(oaLoanApplyEntity.getLoanapplyNo());
		} else {
			loanEntity = o2oLoanapplyService.fingByBillCode(billCode);
		}
		log.info("查询主表对象:{}", loanEntity);
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		if (null == loanEntity) {
			PageResult<Object[]> pageResult = new PageResult<>();
			pageResult.setData(null);
			PaginationInfo datalistInfo = new PaginationInfo(pageResult, titles, "");
			datalistInfo.setBuildPagingNavigation(false);
			datalistInfo.setRendTo("#o2o_file");
			return PaginationRestResult.buildPaginationRestResult(datalistInfo);
		}
		PageResult<Object[]> pageResult = o2oLoanapplyService.findO2oDataListEntityById(loanEntity.getLoanapplyNo(), "'" + code + "'");
		Button[] titleButtons = new Button[] { null, ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo datalistInfo = new PaginationInfo(pageResult, titles, "");
		datalistInfo.setTitleButtons(titleButtons);
		datalistInfo.setBuildPagingNavigation(false);
		datalistInfo.setFirstRenderColumnIndex(1);
		datalistInfo.setBuildRowNumber(true);
		datalistInfo.setRendTo("#o2o_file");
		return PaginationRestResult.buildPaginationRestResult(datalistInfo);
	}

	/**
	 * yy
	 * 
	 * @param loaner
	 * @return
	 */
	public String checkLoaner(CustomerEntity loanerBasic) {
		// CustomerBasicInfo loanerExpansion =
		// o2oLoanapplyService.getCustomerEntityById(loanerBasic.getCustomerId());
		if (StringUtil.isEmpty(loanerBasic.getSex())) { // 借款人性别
			return "借款人性别不能为空，请编辑客户信息";
		}
		if (StringUtil.isEmpty(loanerBasic.getCustomerFamilyName())) { // 借款人姓
			return "借款人姓不能为空，请编辑客户信息";
		}
		if (StringUtils.isEmpty(loanerBasic.getCustomerGivenName())) { // 借款人名
			return "借款人名不能为空，请编辑客户信息";
		}

		if (StringUtils.isEmpty(loanerBasic.getIdType())) { // 证件类型
			return "借款人证件类型不能为空，请编辑客户信息";
		}

		if (StringUtils.isEmpty(loanerBasic.getIdNumber())) { // 证件号码
			return "借款人证件号码不能为空，请编辑客户信息";
		}

		if (loanerBasic.getMaritalStatus() == 0) { // 配偶情况
			return "借款人婚姻情况不能为空，请编辑客户信息";
		}

		if (loanerBasic.getLicenseExpirationDate() == null) { // 证件到期时间
			return "借款人证件到期时间不能为空，请编辑客户信息";
		}

		// List<CustomerUnit> custmenrList =
		// iCustomerInfoService.findCustomerUnit(loanerBasic.getCustomerId());//
		// 合作平台
		//
		// if (null == custmenrList || custmenrList.isEmpty()) {
		// return "借款人合作平台不能为空，请编辑客户信息";
		// }

		if (StringUtils.isEmpty(loanerBasic.getMobileNumber())) { // 联系电话
			return "借款人联系电话不能为空，请编辑客户信息";
		}

		if (StringUtils.isEmpty(loanerBasic.getCustomerName())) { // 客户名称
			return "借款人客户名称不能为空，请编辑客户信息";
		}
		// if (loanerExpansion.getCustomerSd() == null) {
		// return "请完善客户基本信息的门店名称字段";
		// }

		return StatusContexts.SUCCESS;
	}

	/**
	 * yy
	 * 
	 * @param mate
	 * @return
	 */

	public String checkMate(CustomerEntity loanerBasic) {
		if (loanerBasic == null || StringUtil.isBlank(loanerBasic.getCustomerId())) {
			return "借款人信息未完善";
		}
		O2oLoanUserEntity loanUserEntity = o2oLoanapplyService.fingByloanId(loanerBasic.getCustomerId());// 查询客户信息
		CustomerBasicInfo loanerExpansion = o2oLoanapplyService.getCustomerEntityById(loanUserEntity.getId());
		if (StringUtil.isEmpty(loanerBasic.getSex())) { // 借款人性别
			return "借款人性别不能为空，请编辑客户信息";
		}
		if (StringUtil.isEmpty(loanerBasic.getCustomerFamilyName())) { // 配偶姓
			return "配偶姓不能空，请编辑客户信息";
		}
		if (StringUtils.isEmpty(loanerBasic.getCustomerGivenName())) { // 配偶名
			return "配偶名不能为空，请编辑客户信息";
		}

		if (StringUtils.isEmpty(loanerBasic.getIdType())) { // 证件类型
			return "配偶证件类型不能为空，请编辑配偶信息";
		}

		if (StringUtils.isEmpty(loanerBasic.getIdNumber())) { // 证件号码
			return "配偶证件号码不能为空，请编辑配偶信息";
		}

		if (StringUtils.isEmpty(loanerBasic.getMobileNumber())) { // 联系电话
			return "配偶联系电话不能为空，请编辑配偶信息";
		}

		if (loanerExpansion.getLicenseExpirationDate() == null) { // 证件到期时间
			return "配偶证件到期时间不能为空，请编辑配偶信息";
		}
		return StatusContexts.SUCCESS;
	}

	/**
	 * @Description:
	 * 				<p>
	 *               两个string类型的日期比较大小
	 *               </p>
	 * @author CXW
	 * @date 2017年6月16日 下午4:11:24
	 * @param date1作废限制到期时间，date2当前时间
	 * @version V1.0
	 */
	public static int compare_date(String date1, String date2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(date1);
			Date dt2 = df.parse(date2);
			if (dt1.getTime() > dt2.getTime()) {// dt1 在dt2前
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {// dt1在dt2后
				return -1;
			} else {// dt1相等dt2
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

}
